package com.hc.kotlin

import kotlin.reflect.KProperty

interface IRead{
    fun read()
}
class ARead(val r:IRead):IRead by r //委托类

class IntClass{
    var age by IntDelegate()
    var number by IntDelegate2()
}
class IntDelegate(var default:Int = 10){ //委托属性
    //r:被委托对象  p:被委托对象的描述
    operator fun getValue(r:Any?,p:KProperty<*>):Int {
        return default
    }
    operator fun setValue(r:Any?,p:KProperty<*>,v:Int){
        default = v
    }
}

class IntDelegate2(var default:Int = 10){
    operator fun getValue(r:IntClass?,p:KProperty<*>):Int {
        return default
    }
    operator fun setValue(r:IntClass?,p:KProperty<*>,v:Int){
        default = v
    }
}

fun main() {
    var a by IntDelegate()
    println(a)
}